/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file reads the TWFE and Callaway & Sant'Anna ATT estimates,
	saves them (used to create Table C.1), and generates figure 3.
*/

/* NOTE - Global macros for directories are created in the main_text_results.do 
	file which calls this individual .do file */
	
*Create log file
log using "$dr_output_log\figure3", replace

/* Start with Flood to CP or LEPA */
import excel "${dr_temp}\twfe.xlsx", firstrow sheet(floodtocplepa_91_15_nyt) clear
	gen est = "twfe"
	replace lb_estimate = estimate - 1.96*se_estimate
	replace ub_estimate = estimate + 1.96*se_estimate
	replace scaled_estimate = 100*estimate/panel_mean_dep_var
	replace scaled_lb = 100*lb_estimate/panel_mean_dep_var
	replace scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate
	save "${dr_temp}\floodtocplepa_twfe.dta", replace
	
import excel "${dr_temp}/cs_floodtocporlepa_91_15_nyt.xlsx", firstrow sheet(cs_att) clear
	keep if effect_estimated=="group_agg_effect_average"
	gen est = "cs"
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	gen scaled_lb = 100*ci_95_lb/panel_mean_dep_var
	gen scaled_ub = 100*ci_95_ub/panel_mean_dep_var
	rename (ci_95_lb ci_95_ub) (lb_estimate ub_estimate)
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate
	save "${dr_temp}\floodtocplepa_cs.dta", replace

	*Append the TWFE results 
	append using "${dr_temp}\floodtocplepa_twfe.dta"

******* Table C.1 - ATT estimates in levels for TWFE and CS estimators *********
	*Generate column indicating tech transition and save
	gen transition = "flood_to_cp"
	save "${dr_output_app}\floodtocplepa_att_twfecs.dta", replace 
	
/* Now CP to LEPA */
import excel "${dr_temp}\twfe.xlsx", firstrow sheet(cptolepa_91_19) clear
	gen est = "twfe"
	replace lb_estimate = estimate - 1.96*se_estimate
	replace ub_estimate = estimate + 1.96*se_estimate
	replace scaled_estimate = 100*estimate/panel_mean_dep_var
	replace scaled_lb = 100*lb_estimate/panel_mean_dep_var
	replace scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate
	save "${dr_temp}\cptolepa_twfe.dta", replace
	
import excel "${dr_temp}/cs_cptolepa.xlsx", firstrow sheet(cs_att) clear
	keep if effect_estimated=="group_agg_effect_average"
	gen est = "cs"
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	gen scaled_lb = 100*ci_95_lb/panel_mean_dep_var
	gen scaled_ub = 100*ci_95_ub/panel_mean_dep_var
	rename (ci_95_lb ci_95_ub) (lb_estimate ub_estimate)
	keep dep_var est scaled_estimate scaled_lb scaled_ub estimate se_estimate lb_estimate ub_estimate
	save "${dr_temp}\cptolepa_cs.dta", replace
	
	*Append the TWFE results 
	append using "${dr_temp}\cptolepa_twfe.dta"

******* Table C.1 - ATT estimates in levels for TWFE and CS estimators *********
	*Generate column indicating tech transition and save
	gen transition = "cp_to_lepa"
	save "${dr_output_app}\cptolepa_att_twfecs.dta", replace 

********************************************************************************
** Figure 3 - Plot ATT estimates in percent terms for TWFE and CS estimators ***
********************************************************************************
*Combine all estimated ATTs for all estimators and transitions
	append using "${dr_output_app}\floodtocplepa_att_twfecs.dta"
	*Make numerical variable encoding estimator 
	gen estimator = 1 if est=="cs"
	replace estimator = 2 if est=="twfe"
	drop if est=="cd"

/* Create plot for each dependent variable */
	*Acre-ft
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="af_used", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="af_used", mcolor("navy") msize(small)), ///  
		title("Acre-feet withdrawn", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		text(23 .3 "A. Flood to center pivot", place(e) size(medium)) ///
		xsize(2.5) ysize(3) ///
		name(flood_cp_acreft, replace)		
		
	*Acres irrigated
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="acres_irr", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="acres_irr", mcolor("navy") msize(small)), ///  
		title("Acres irrigated", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(flood_cp_acresirr, replace)
		
	*Depth applied, estimates that passed placebo in navy, failed in red
	tw (rcap scaled_lb scaled_ub estimator if transition=="flood_to_cp" & ///
		dep_var=="depth_applied", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="flood_to_cp" & ///
		dep_var=="depth_applied", mcolor("navy") msize(small)), ///  
		title("Depth applied", size(mediumsmall)) ///
		yscale(range(-15 15)) ylabel(-15(5)15, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(flood_cp_depthapp, replace)
		
	*Combine all the flood to cp graphs 
		graph combine flood_cp_acreft flood_cp_acresirr flood_cp_depthapp, ///
		rows(1) cols(3) ///
		l1title("Percent change", size(small)) ///
		xsize(6.5) ysize(2.65) name(flood_cp_all, replace)
			
**# CP to cp with Lepa
	*Acre-ft, estimates that passed placebo in navy, failed in red
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="af_used", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="af_used", mcolor("navy") msize(small)), ///  
		title("Acre-feet withdrawn", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		text(8.8 .29 "B. Traditional center pivot to LEPA", place(e) size(medium)) ///
		xsize(2.5) ysize(3) ///
		name(cp_lepa_acreft, replace) 
		
	*Acres irrigated, estimates that passed placebo in navy, failed in red
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="acres_irr", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="acres_irr", mcolor("navy") msize(small)), ///  
		title("Acres irrigated", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) /// 
		xsize(2) ysize(3) ///
		name(cp_lepa_acresirr, replace)
		
	*Depth applied, estimates that passed placebo in navy, failed in red
	tw (rcap scaled_lb scaled_ub estimator if transition=="cp_to_lepa" & ///
		dep_var=="depth_applied", lcolor("navy")) ///
		(scatter scaled_estimate estimator if transition=="cp_to_lepa" & ///
		dep_var=="depth_applied", mcolor("navy") msize(small)), ///  
		title("Depth applied", size(mediumsmall)) ///
		yscale(range(-10 5)) ylabel(-10(2.5)5, labsize(small)) xscale(range(.8 2.2)) ///
		legend(off) ///
		yline(0, lpattern(solid)) ///
		ytitle("") xtitle("") ///
		xlabel(1 "{it}{&delta}{superscript:CS}" 2 "{it}{&beta}{superscript:TWFE}", labsize(medium)) ///
		graphregion(color(white) margin (1 1 1 8)) ///
		xsize(2) ysize(3) ///
		name(cp_lepa_depthapp, replace)
		
	*Combine all the cp to LEPA graphs
		graph combine cp_lepa_acreft cp_lepa_acresirr cp_lepa_depthapp, ///
		rows(1) cols(3) ///
		l1title("Percent change", size(small)) ///
		xsize(6.5) ysize(2.65) name(cp_lepa_all, replace)
	
*Combine the graphs 	
graph combine flood_cp_all cp_lepa_all, ///
	rows(2) cols(1) ///
	b1title("Estimator", size(small)) ///
	xsize(6.5) ysize(8) 
graph export "${dr_output_main}\figure3.tif", width(6500) height(7800) replace 

*Close log
log close